package com.pb.infra.auth.controller;

import com.pb.infra.auth.service.PriviStructService;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.ImportExcel;
import com.pb.infra.oth.util.JwtTokenConfig;
import com.pb.infra.oth.util.SysLog;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 组织架构管理
 *
 * @author liudebao
 * @date 2020/12/25
 */
@Slf4j
@RestController
@RequestMapping("v1/system/struct")
public class PriviStructController {

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "PRIVI_STRUCT";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "组织架构管理";

    /**
     * 业务逻辑对象
     */
    @Resource
    private PriviStructService priviStructService;

    @Resource
    private JwtTokenConfig jwtTokenConfig;

    /**
     * 查询组织架构
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> paramMap) {
        return priviStructService.select(paramMap);
    }


    /**
     * 新建组织架构信息
     *
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        return priviStructService.insert(map, request);
    }


    /**
     * 修改组织架构信息
     *
     * @param map 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        priviStructService.update(map, request);
        return Result.OK_EMPTY;
    }

    /**
     * 删除组织架构信息
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> paramMap) {
        if (priviStructService.delete(paramMap)) {
            return Result.error("-1", "某用户的职位包含选中的组织");
        } else {
            return Result.OK_EMPTY;
        }
    }

    /**
     * 恢复组织架构
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("updateFlag")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result updateFlag(@RequestBody List<String> paramMap) {
        priviStructService.updateFlag(paramMap);
        return Result.OK_EMPTY;
    }

    /**
     * 导出
     * @param paramMap
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/exportAllOrg")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.EXP, menuName = OPERATION_MENU)
    public Map<String, Object> exportColumnExcel(@RequestBody  Map<String, Object> paramMap, HttpServletRequest request, HttpServletResponse response) {
        String token = request.getHeader(jwtTokenConfig.getHeader());
        List<Map<String,Object>> list=priviStructService.getAllOrg();
        paramMap.clear();
        String[][] strArr={{"组织名称",  "组织标识", "父级标识", "组织简称", "组织类型", "组织属性标识", "组织属性名称", "所属法人标识", "所属法人名称"}};
        String[][] nameArr={{"structName","structId", "parentId","structLabel","structType","structAttr","attrName","corpId","corpName"}};
        String[][] dataType = {{"STRING","STRING","STRING","STRING","STRING","STRING","STRING","STRING","STRING"}};
        String[][] comments={{"",  "", "", "", "1-机构；2-部门；3-虚拟机构", "", "", "", ""}};
        Object[] obj={list};
        String[] jobBookName={"组织架构"};
        paramMap.put("jobBookName",jobBookName);
        paramMap.put("title",strArr);
        paramMap.put("order",nameArr);
        paramMap.put("comments",comments);
        paramMap.put("token",token);
        paramMap.put("data",obj);
        paramMap.put("dataType",dataType);
        paramMap.put("fileName","组织架构");
        return  paramMap;
    }

    /**
     * 导入
     * @param paramMap
     * @return
     */
    @PostMapping("importExcel")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.IMP, menuName = OPERATION_MENU)
    public Result importExcel(@ImportExcel() Map<String,Object> paramMap, HttpServletRequest request){
        try {
            Integer code= (Integer) paramMap.get("code");//得到校验结果
            //code=1标识数据正确
            if (code==1){
                String token = request.getHeader(jwtTokenConfig.getHeader());
                Claims claims = jwtTokenConfig.getClaimByToken(token);
                String userId = (String)claims.get(jwtTokenConfig.getSub());
                List<List<Map<String,Object>>> data = ( List<List<Map<String,Object>>>) paramMap.get("data"); //得到Excel中的所有数据
                String msg=priviStructService.importExcel(data, userId);
                if (!"".equals(msg)){
                    paramMap.put("msg",msg+paramMap.get("msg"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            paramMap.put("msg","导入遇到异常请联系管理员");
        }
        paramMap.remove("data");
        return Result.ok(paramMap);
    }

}
